package belf.migrate.engine.ddl;

import belf.migrate.plugin.oracle.antlr.PlSqlLexer;
import belf.migrate.plugin.oracle.antlr.PlSqlParser;
import belf.migrate.plugin.oracle.antlr.PlSqlParserBaseListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static java.nio.file.Files.readAllLines;

public class OracleFunctionCheck {


    public void main(String[] args) throws IOException {
        String sql = String.join("\n", readAllLines(Paths.get(args[0])));
        CharStream input = CharStreams.fromString(sql);

        // 创建词法分析器和解析器
        PlSqlLexer lexer = new PlSqlLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PlSqlParser parser = new PlSqlParser(tokens);

        // 从解析器中获取起始规则并进行解析
        PlSqlParser.Sql_scriptContext tree = parser.sql_script();
        ParseTreeWalker walker = new ParseTreeWalker();
//        FunctionCheckListener listener = new FunctionCheckListener();
//        walker.walk(listener, tree);
    }


    private static class FunctionCheckListener extends PlSqlParserBaseListener {
        private static List<String> DIFFIRENT_FUNCTIONS = Arrays.asList("NCHR", "REGEXP_COUNT ");
        @Override
        public void visitTerminal(TerminalNode node) {
            String text = node.getText().trim();
            if (DIFFIRENT_FUNCTIONS.contains(text) && BaseDDLConvert.hasAncestor("Compound_expressionContext", node)) {

            }
        }
    }
}
